set(LOCAL_REGISTRY local_registry)
set(LOCAL_REGISTRY_CTRL local_registry_ctrl)
set(IPC_HV_SOA ipc_hv_soa)
set(IPC_HV_SOA_CLIENT1 ipc_hv_soa_client1)
set(IPC_HV_SOA_CLIENT2 ipc_hv_soa_client2)

if(NOT CPUNCHLINE_BUILD_THIRDPARTY)
    find_package(libhv REQUIRED)
endif()

nanopb_generate_cpp(TARGET ${LOCAL_REGISTRY}_nanopb_auto proto/${LOCAL_REGISTRY}.proto)

# daemon
add_executable(${LOCAL_REGISTRY}
    daemon/local_registry.cpp
)
target_include_directories(${LOCAL_REGISTRY} PRIVATE inc)
target_link_libraries(${LOCAL_REGISTRY} PRIVATE base hv ${LOCAL_REGISTRY}_nanopb_auto)

install(TARGETS ${LOCAL_REGISTRY} DESTINATION bin)

# ctrl
add_executable(${LOCAL_REGISTRY_CTRL}
    ctrl/local_registry_ctrl.cpp
)
target_include_directories(${LOCAL_REGISTRY_CTRL} PRIVATE inc)
target_link_libraries(${LOCAL_REGISTRY_CTRL} PRIVATE base hv ${LOCAL_REGISTRY}_nanopb_auto)

install(TARGETS ${LOCAL_REGISTRY_CTRL} DESTINATION bin)

# lib
add_library(${IPC_HV_SOA}-deps INTERFACE)
target_include_directories(${IPC_HV_SOA}-deps INTERFACE inc)
target_include_directories(${IPC_HV_SOA}-deps INTERFACE lib)
target_link_libraries(${IPC_HV_SOA}-deps INTERFACE hv)

add_library(${IPC_HV_SOA}-object OBJECT
    lib/ipc_hv_soa.cpp
)
target_link_libraries(${IPC_HV_SOA}-object PRIVATE ${IPC_HV_SOA}-deps)
target_link_libraries(${IPC_HV_SOA}-object PRIVATE base ${LOCAL_REGISTRY}_nanopb_auto)

if(BUILD_SHARED_LIBS)
    add_library(${IPC_HV_SOA} SHARED $<TARGET_OBJECTS:${IPC_HV_SOA}-object>)
else()
    add_library(${IPC_HV_SOA} STATIC $<TARGET_OBJECTS:${IPC_HV_SOA}-object>)
endif()

target_link_libraries(${IPC_HV_SOA} INTERFACE ${IPC_HV_SOA}-deps)
target_link_libraries(${IPC_HV_SOA} PRIVATE base)
set_property(TARGET ${IPC_HV_SOA} PROPERTY OUTPUT_NAME "${IPC_HV_SOA}")
set_property(TARGET ${IPC_HV_SOA} PROPERTY VERSION 0.0.1)
set_property(TARGET ${IPC_HV_SOA} PROPERTY SOVERSION 0)

if(WIN32)
    set_property(TARGET ${IPC_HV_SOA} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})
    set_property(TARGET ${IPC_HV_SOA} PROPERTY LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})
endif()

install(TARGETS ${IPC_HV_SOA} DESTINATION lib)
install(FILES lib/ipc_hv_soa.h DESTINATION include/${IPC_HV_SOA})

# client
add_executable(${IPC_HV_SOA_CLIENT1}
    client/ipc_hv_soa_client1.c
)
target_include_directories(${IPC_HV_SOA_CLIENT1} PRIVATE inc)
target_link_libraries(${IPC_HV_SOA_CLIENT1} PRIVATE base ${IPC_HV_SOA} ${LOCAL_REGISTRY}_nanopb_auto)

install(TARGETS ${IPC_HV_SOA_CLIENT1} DESTINATION bin)

add_executable(${IPC_HV_SOA_CLIENT2}
    client/ipc_hv_soa_client2.c
)
target_include_directories(${IPC_HV_SOA_CLIENT2} PRIVATE inc)
target_link_libraries(${IPC_HV_SOA_CLIENT2} PRIVATE base ${IPC_HV_SOA} ${LOCAL_REGISTRY}_nanopb_auto)

install(TARGETS ${IPC_HV_SOA_CLIENT2} DESTINATION bin)
